//
//  LGCTrackTableViewCell.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 L-G. All rights reserved.
//

#import "LGCTrackTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>

#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@implementation LGCTrackTableViewCell
- (void)dealloc
{
    [_labelDay release];
    [_labelDuration release];
    [_labelHistoryCount release];
    [_labelLike release];
    [_labelNickName release];
    [_labelTitle release];
    [_imageDuration release];
    [_imageHistory release];
    [_imageLike release];
    [_imagePic release];
    [_imagePlay release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.imagePic = [[UIImageView alloc] init];
        self.imagePic.layer.masksToBounds = YES;
        self.imagePic.layer.cornerRadius = 40 * HEIGHT;
        [self.contentView addSubview:self.imagePic];
        [_imagePic release];
        
        self.labelTitle = [[UILabel alloc] init];
        self.labelTitle.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.labelTitle];
        [_labelTitle release];
        
        self.labelNickName = [[UILabel alloc] init];
        self.labelNickName.font = [UIFont systemFontOfSize:13];
        self.labelNickName.textColor = [UIColor grayColor];
        [self.contentView addSubview:self.labelNickName];
        [_labelNickName release];
        
        self.labelDay = [[UILabel alloc] init];
        self.labelDay.textColor = [UIColor grayColor];
        self.labelDay.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:self.labelDay];
        [_labelDay release];
        
        self.labelHistoryCount = [[UILabel alloc] init];
        self.labelHistoryCount.textColor = [UIColor grayColor];
        self.labelHistoryCount.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.labelHistoryCount];
        [_labelHistoryCount release];
        
        self.labelLike = [[UILabel alloc] init];
        self.labelLike.font = [UIFont systemFontOfSize:12];
        self.labelLike.textColor = [UIColor grayColor];
        [self.contentView addSubview:self.labelLike];
        [_labelLike release];
        
        self.labelDuration = [[UILabel alloc] init];
        self.labelDuration.textColor = [UIColor grayColor];
        self.labelDuration.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.labelDuration];
        [_labelDuration release];
        
        self.imagePlay = [[UIImageView alloc] init];
        self.imagePlay.image = [UIImage imageNamed:@"play@2x"];
        [self.contentView addSubview:self.imagePlay];
        [_imagePlay release];
        
        self.imageHistory = [[UIImageView alloc] init];
        self.imageHistory.image = [UIImage imageNamed:@"videoplay"];
        [self.contentView addSubview:self.imageHistory];
        [_imageHistory release];
        
        self.imageLike = [[UIImageView alloc] init];
        self.imageLike.image = [UIImage imageNamed:@"duanzi_up@2x"];
        [self.contentView addSubview:self.imageLike];
        [_imageLike release];
        
        self.imageDuration = [[UIImageView alloc] init];
        self.imageDuration.image = [UIImage imageNamed:@"duration"];
        [self.contentView addSubview:self.imageDuration];
        [_imageDuration release];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imagePic.frame = CGRectMake(15 * WIDTH, 10 * HEIGHT, 80 * WIDTH, 80 * HEIGHT);
    self.labelTitle.frame = CGRectMake(108 * WIDTH, 5 * HEIGHT, self.frame.size.width / 3 * 2 - 30 * WIDTH, 40 * HEIGHT);
    self.labelDay.frame = CGRectMake(77 * WIDTH + self.frame.size.width / 3 * 2, 20 * HEIGHT, 45 * WIDTH, 15 * HEIGHT);
    self.labelNickName.frame = CGRectMake(110 * WIDTH, 50 * HEIGHT, self.frame.size.width / 3 * 2, 10 * HEIGHT);
    self.imageHistory.frame = CGRectMake(110 * WIDTH, 75 * HEIGHT, 15 * WIDTH, 15 * HEIGHT);
    self.labelHistoryCount.frame = CGRectMake(130 * WIDTH, 75 * HEIGHT, 55 * WIDTH, 15 * HEIGHT);
    self.imageLike.frame = CGRectMake(185 * WIDTH, 75 * HEIGHT, 15 * WIDTH, 15 * HEIGHT);
    self.labelLike.frame = CGRectMake(205 * WIDTH, 76 * HEIGHT, 50 * WIDTH, 15 * HEIGHT);
    self.imageDuration.frame = CGRectMake(250 * WIDTH, 75 * HEIGHT, 15 * WIDTH, 15 * HEIGHT);
    self.labelDuration.frame = CGRectMake(270 * WIDTH, 75 * HEIGHT, 100 * WIDTH, 15 * HEIGHT);
    self.imagePlay.frame = CGRectMake(40 * WIDTH, 35 * HEIGHT, 30 * WIDTH, 30 * HEIGHT);
}

- (void)setTrackModel:(TrackModel *)trackModel
{
    if (_trackModel != trackModel) {
        [_trackModel release];
        _trackModel = [trackModel retain];
    }
    self.labelTitle.text = trackModel.title;
    
    self.labelNickName.text = trackModel.nickname;
    /***************** 计算时间 *****************/
  
    NSString * timeStampString = [NSString stringWithFormat:@"%@",trackModel.createdAt];
    
    NSTimeInterval _interval=[[timeStampString substringToIndex:10] doubleValue];
   
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSString *str = [NSString stringWithFormat:@"%@",date];
    NSRange range = NSMakeRange(5, 5);
    NSString *stry = [str substringWithRange:range];
     self.labelDay.text = stry;
    /***************** ￥￥￥￥￥￥￥ *****************/
    NSURL *url = [NSURL URLWithString:trackModel.coverLarge];
    [self.imagePic sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    if ([trackModel.playtimes floatValue] >= 10000) {
        self.labelHistoryCount.text = [NSString stringWithFormat:@"%.2f万",[trackModel.playtimes floatValue] / 10000];
    }else
    {
        self.labelHistoryCount.text = trackModel.playtimes.description;
    }
    self.labelLike.text = trackModel.likes.description;
    
    self.labelDuration.text = [NSString stringWithFormat:@"%02ld:%02ld",[trackModel.duration integerValue] / 60,[trackModel.duration integerValue] % 60];
}
+ (LGCTrackTableViewCell *)trackTableViewWith:(UITableView *)tableView
{
    static NSString *cellIdentify = @"cellLGC";
    LGCTrackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (cell == nil) {
        cell = [[LGCTrackTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify];
    }
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
